package com.disk.controller;

import com.disk.domain.FileDetail;
import com.disk.domain.FileInformation;
import com.disk.handleResult.FileResult;
import com.disk.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;


@RestController
@RequestMapping("/file")
@PreAuthorize("hasAuthority('manage:file')")
public class FileController {
    @Autowired
    private FileService fileService;

    @Operation(summary = "通过Authentication获取" +
            "JwtAuthenticationTokenFilter中设置好的principal:userLoginDetail.getUser().getId()")
    @GetMapping("/get")
    public Object getCurrentUserName(Authentication authentication){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        fileService.setUserRoot(userDetails.getUsername());
        return userDetails.getUsername();
    }

    @PostMapping("/upload")
    @Operation(summary = "在给定folderName路径下上传单个文件")
    public FileResult upload(@RequestParam String folderName, @RequestParam("file") MultipartFile file) {
       return fileService.upload(folderName,file);
    }

    @Operation(summary = "批量 在给定folderName路径下上传多个文件")
    @PostMapping("/batch/upload")
    public FileResult uploadFiles(@RequestParam String folderName, @RequestParam("file") List<MultipartFile> files) {
        return fileService.uploadFiles(folderName,files);
    }

    @Operation(summary = "删除指定文件夹的单个文件")
    @GetMapping("/delete")
    public FileResult delete(@RequestParam String folderName, @RequestParam String fileName) {
        return fileService.delete(folderName,fileName);
    }

    @Operation(summary = "批量 删除指定文件夹的多个文件")
    @PostMapping("/batch/delete")
    public FileResult deleteFiles(@RequestBody FileDetail[] files) {
        return fileService.deleteFiles(files);
    }

    @Operation(summary = "给出FileInformation对象，修改文件(重命名)")
    @PostMapping("/update")
    public FileResult updateFile(@RequestBody FileInformation fileInformation) {
        return fileService.updateFile(fileInformation);
    }

    @GetMapping("/down")
    @Operation(summary = "下载指定文件夹下的文件")
    public FileResult tryDown(@RequestParam String folderName, @RequestParam String fileName){
        return fileService.tryDown(folderName,fileName);
    }

}
